// VBConversions Note: VB project level imports
using System.Diagnostics;
using System;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using Microsoft.VisualBasic;
using System.Data;
using System.Collections.Generic;
using Microsoft.VisualBasic.Compatibility;
using System.Linq;
// End of VB project level imports

using System.Security.Cryptography;
using System.Text;


namespace 喷码数据生成工具
{
	public class DES
	{
		public string GenerateKey()
		{
			DESCryptoServiceProvider desCrypTo = DESCryptoServiceProvider.Create;
			return ASCIIEncoding.ASCII.GetString(desCrypTo.Key);
		}
		public string Encrypt(string pToEncrypt, string sKey)
		{
			DESCryptoServiceProvider Des = new DESCryptoServiceProvider();
			byte[] InputByteArray = null;
			byte[] KeyArray = null;
			InputByteArray = Encoding.Default.GetBytes(pToEncrypt);
			KeyArray = ASCIIEncoding.ASCII.GetBytes(sKey);
			// Des.KeySize = sKey.Length
			Des.Key = KeyArray;
			Des.IV = KeyArray;
			System.IO.MemoryStream ms = new System.IO.MemoryStream();
			CryptoStream cs = new CryptoStream(ms, Des.CreateEncryptor(), CryptoStreamMode.Write);
			cs.Write(InputByteArray, 0, InputByteArray.Length);
			cs.FlushFinalBlock();
			StringBuilder ret = new StringBuilder();
			byte b = 0;
			foreach (byte tempLoopVar_b in ms.ToArray())
			{
				b = tempLoopVar_b;
				ret.AppendFormat("{0:X2}", b);
			}
			return ret.ToString();
		}
		public string Decrypt(string pToDecrypt, string sKey)
		{
			try
			{
				DESCryptoServiceProvider des = new DESCryptoServiceProvider();
				int len = 0;
				len = (int) ((double) pToDecrypt.Length / 2 - 1);
				if (len == 0)
				{
					len = 1;
				}
				byte[] InputByteArray = new byte[len + 1];
				int x = 0;
				int i = 0;
				for (x = 0; x <= len; x++)
				{
					i = Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16);
					InputByteArray[x] = System.Convert.ToByte(i);
				}
				des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
				des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
				System.IO.MemoryStream ms = new System.IO.MemoryStream();
				CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
				cs.Write(InputByteArray, 0, InputByteArray.Length);
				cs.FlushFinalBlock();
				return Encoding.Default.GetString(ms.ToArray());
			}
			catch
			{
				return "";
			}
		}
	}
	
}
